package com.example.hellomap;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Alle
 *
 */
public class MyLocationListener implements LocationListener {
	
	Activity parentActivity;
	private String provider;
	private LocationManager locationManager;	
	TextView textProviderInfo;
	TextView textLatLngInfo;
	private double lat;
	private double lng;

	
	
	public Activity getParentActivity() {
		return parentActivity;
	}

	public String getProvider() {
		return provider;
	}

	public LocationManager getLocationManager() {
		return locationManager;
	}

	public MyLocationListener(MainActivity activity) {
		parentActivity = activity;
		textProviderInfo = (TextView)parentActivity.findViewById(R.id.info);	
		textLatLngInfo = (TextView)parentActivity.findViewById(R.id.locinfo);
	}
	
	void initLocation() {
		
		// Get the location manager
	    locationManager = (LocationManager) parentActivity.getSystemService(Context.LOCATION_SERVICE) ;
	    
	    // Define the criteria how to select the locatioin provider -> use default
	    Criteria criteria = new Criteria();
	    criteria.setAltitudeRequired(false);
	    provider = locationManager.getBestProvider(criteria, false);
	    Location location = locationManager.getLastKnownLocation(provider);

	    // Initialize the location fields
	    if (location != null) {
	      System.out.println("Provider " + provider + " has been selected.");
	      textProviderInfo.setText("Provider " + provider + " has been selected.");
	      onLocationChanged(location);
	    } else {
	    	Toast.makeText( parentActivity, "Location not available " , Toast.LENGTH_LONG).show();
	    }
	}
	
	void requestLocationUpdates() {		
		locationManager.requestLocationUpdates(provider, 400, 1, this);
	}
	
	void removeUpdates() {
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		lat = location.getLatitude();
	    lng = location.getLongitude();
	    /*Toast.makeText( parentActivity, "Loc: \n" + String.valueOf(lat) + ", " 
	    										+ String.valueOf(lng), 
	    										Toast.LENGTH_LONG).show();   */
	    textLatLngInfo.setText("Your location: \n" + String.valueOf(lat) + ", " + String.valueOf(lng) );
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(parentActivity, "Disabled provider " + provider,
		        Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(parentActivity, "Enabled new provider " + provider,
		        Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Get Last Latitude position in double
	 * @return
	 */
	public double getLat() {
		return lat;
	}

	/**
	 * Get Last longitude position in double
	 * @return
	 */
	public double getLng() {
		return lng;
	}


}
